<?php

/**
 * @BelongProject: flami_mangers
 * @Author: Zwc
 * @CreateTime: 2025-11-08 15:38:10
 * @Description: TODO
 */

namespace app\admin\service;

use app\admin\model\TaskModel;
use think\facade\Log;

class TaskService
{
    private const TaskType = [
        [
            'type' => 'uploadAvatar',
            'name' => '上传头像',
            'isNeedCond' => 0,
            'completeStr' => '上传头像%s次'
        ],[
            'type' => 'userAuth',
            'name' => '实名认证',
            'isNeedCond' => 0,
            'completeStr' => '完成实名认证%s次'
        ],[
            'type' => 'updateUserInfo',
            'name' => '完善资料',
            'isNeedCond' => 0,
            'completeStr' => '完善资料%s次'
        ],[
            'type' => 'videoCall',
            'name' => '视频通话',
            'isNeedCond' => 1,
            'completeStr' => '每日视频通话%s分钟'
        ],[
            'type' => 'startLive',
            'name' => '开启直播',
            'isNeedCond' => 1,
            'completeStr' => '每日开启直播%s分钟'
        ],[
            'type' => 'publishDynamic',
            'name' => '发布动态',
            'isNeedCond' => 1,
            'completeStr' => '每日发布动态%s条'
        ],[
            'type' => 'dynamicZan',
            'name' => '点赞动态',
            'isNeedCond' => 1,
            'completeStr' => '每日点赞动态%s次'
        ],[
            'type' => 'sendMessage',
            'name' => '发送私信',
            'isNeedCond' => 1,
            'completeStr' => '每日发送私信%s人次'
        ],
        [
            'type' => 'sign',
            'name' => '每日签到',
            'isNeedCond' => 0,
            'completeStr' => '每日签到%s次'
        ]
    ];
    
    /**
     * 列表
     * @param $params
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public static function getData($params): \think\Paginator
    {
        $condition = [
            ['del_flag','=',0],
        ];

        if (!empty($params['langId'])) {
            $condition[] = ['lang_id','=',$params['langId']];
        }
        $model = new TaskModel();
        $data = $model->where($condition)->paginate(20);
        foreach (self::TaskType as $val) {
            $taskTypeMap[$val['type']] = [
                'name' => $val['name'],
                'completeStr' => $val['completeStr'],
            ];
        }

        foreach ($data as &$val) {
            if (isset($taskTypeMap[$val['task_type']])) {
                $val['typeName'] = $taskTypeMap[$val['task_type']]['name'];
                $val['completeStr'] = sprintf($taskTypeMap[$val['task_type']]['completeStr'], $val['complete_count']);
            }else {
                $val['typeName'] = '';
                $val['completeStr'] = '';
            }
        }
        return $data;
    }

    public function add($params): array
    {
        // 判断任务是否已经存在
        $condition = [
            ['del_flag','=',0],
            ['lang_id','=',$params['lang_id']],
            ['task_group','=',$params['task_group']],
            ['title', '=', $params['title']],
        ];
        $model = new TaskModel();
        $data = $model->where($condition)->find();
        if (!empty($data)) {
            return ['code' => -1, 'msg' => '任务已经存在'];
        }
        $insertData = [
            'lang_id' => $params['lang_id'],
            'task_group' => $params['task_group'],
            'task_group_cn' => $params['task_group_cn'],
            'task_type' => $params['task_type'],
            'title' => $params['title'],
            'complete_count' => $params['complete_count'],
            'award' => $params['award'],
            'task_intro' => $params['task_intro'],
            'task_intro_cn' => $params['task_intro_cn'],
            'status' => $params['status'],
        ];
        try {
            $model->insert($insertData);
            return ['code' => 0, 'msg' => '操作成功'];
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            Log::error($e->getTraceAsString());
            return ['code' => 0, 'msg' => '操作失败，请联系管理员'];
        }
    }

    public function modify($params): array
    {
        // 判断任务是否已经存在
        $condition = [
            ['del_flag','=',0],
            ['lang_id','=',$params['lang_id']],
            ['task_group','=',$params['task_group']],
            ['title', '=', $params['title']],
            ['id', '<>', $params['id']],
        ];
        $model = new TaskModel();
        $data = $model->where($condition)->find();
        if (!empty($data)) {
            return ['code' => -1, 'msg' => '任务已经存在'];
        }
        $insertData = [
            'lang_id' => $params['lang_id'],
            'task_group' => $params['task_group'],
            'task_group_cn' => $params['task_group_cn'],
            'task_type' => $params['task_type'],
            'title' => $params['title'],
            'complete_count' => $params['complete_count'],
            'award' => $params['award'],
            'task_intro' => $params['task_intro'],
            'task_intro_cn' => $params['task_intro_cn'],
            'status' => $params['status']
        ];
        try {
            $model->where(['id' => $params['id']])->data($insertData)->save();
            return ['code' => 0, 'msg' => '操作成功'];
        }catch (\Exception $e) {
//            Log::error($e->getMessage());
//            Log::error($e->getTraceAsString());
            return ['code' => 0, 'msg' => '操作失败，请联系管理员'];
        }
    }

    /**
     * 查看详情
     * @param $id
     * @return TaskModel|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function detail($id)
    {
        $model = new TaskModel();
        $where = [
            ['id' , '=', $id],
        ];

        return $model->where($where)->find();
    }

    /**
     * 删除
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del($id) {
        $model = new TaskModel();
        $task = $model->where(['id' => $id])->find();
        try {
            $task['del_flag'] = 1;
            $task->save();
            return ['code' => 0, 'msg' => '操作成功'];
        }catch (\Exception $e) {
            \think\facade\Log::error($e->getMessage());
            \think\facade\Log::error($e->getTraceAsString());
            return ['code' => -1, 'msg' => '操作失败'];
        }
    }

    /**
     * 获取任务类型
     * @return array[]
     */
    public static function getTypeData(): array
    {
        return self::TaskType;
    }



}